package com.diabetes.controller.user;

import com.diabetes.result.Result;
import com.diabetes.service.DeviceDataService;
import com.huaweicloud.sdk.core.auth.AbstractCredentials;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.*;
import com.huaweicloud.sdk.iotda.v5.model.*;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user/device_data")
@Slf4j
@Api(tags = "设备相关接口")
public class DeviceController {
    @Autowired
    private DeviceDataService deviceDataService;

    @PostMapping("/ON")
    @ApiOperation("设备命令，打开设备")
    public Result deviceON() {
        log.info("设备命令，打开设备");
        String ak = "0QOEBQ4Y9GQ4YRFKH8QF";
        String sk = "FUgLdQyiLwmtQgDYjjeecfuu2HUHEx4DB2tto5h1";
        ICredential auth = new BasicCredentials()
                .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                .withAk(ak)
                .withSk(sk);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf("cn-north-4"))
                .build();
        CreateCommandRequest request = new CreateCommandRequest();
        request.withDeviceId("65eab957fb8177243a4cdfc3_123456");
        DeviceCommandRequest body = new DeviceCommandRequest();
        body.withParas("{\"Control\":\"ON\"}");
        body.withCommandName("Image_Control");
        body.withServiceId("saodi");
        request.withBody(body);
        try {
            CreateCommandResponse response = client.createCommand(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
            return Result.success();
        }
        return Result.success();
    }

    @PostMapping("/OFF")
    @ApiOperation("设备命令，关闭设备")
    public Result deviceOFF() {
        log.info("设备命令，关闭设备");
        String ak = "0QOEBQ4Y9GQ4YRFKH8QF";
        String sk = "FUgLdQyiLwmtQgDYjjeecfuu2HUHEx4DB2tto5h1";
        ICredential auth = new BasicCredentials()
                .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                .withAk(ak)
                .withSk(sk);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf("cn-north-4"))
                .build();
        CreateCommandRequest request = new CreateCommandRequest();
        request.withDeviceId("65eab957fb8177243a4cdfc3_123456");
        DeviceCommandRequest body = new DeviceCommandRequest();
        body.withParas("{\"Control\":\"OFF\"}");
        body.withCommandName("Image_Control");
        body.withServiceId("saodi");
        request.withBody(body);
        try {
            CreateCommandResponse response = client.createCommand(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
            return Result.success();
        }
        return Result.success();
    }

    @PostMapping("/getProperty")
    @ApiOperation("获取设备属性值")
    public Result getDeviceProperty(@RequestBody Map<String, Object> requestData) {
        log.info("获取设备属性值:{}", requestData);
        deviceDataService.devicePost(requestData);
        return Result.success();
    }
}
